package com.wms.wms.controller.query;

import com.wms.wms.service.ExistingService;
import com.wms.wms.service.InStockService;
import com.wms.wms.service.OutStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@Controller

public class QueryController {

    @Autowired
    OutStockService outStockService = new OutStockService();
    @Autowired
    InStockService inStockService = new InStockService();
    @Autowired
    ExistingService existingService = new ExistingService();
    @ResponseBody
    @PostMapping("/query/data")
    public ModelAndView index(@RequestParam(value = "id",required = false) String id,
                              @RequestParam(value = "type_id",required = false) String typeId,
                              @RequestParam(value = "b_date",required = false) String bDate,
                              @RequestParam(value = "e_date",required = false) String eDate,
                              @RequestParam(value = "p",required = false) String p,
                              @RequestParam(value = "i",required = false) String i,
                              @RequestParam(value = "c",required = false) String c,
                              @RequestParam(value = "d",required = false) String d,
                              @RequestParam(value = "t",required = false) String t,
                              @RequestParam(value = "o",required = false) String o
    ){

        ModelAndView modelAndView = new ModelAndView("/query/result");
        try {
            if(!bDate.equals("") && !eDate.equals("")){  // 某段时间
                Date b = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(bDate).getTime());
                Date e = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(eDate).getTime());
                // 销售量
                if(c != null) modelAndView.addObject("salesCountId",outStockService.salesCountId(id,b,e,1));
                // 销毁量
                if(d != null) modelAndView.addObject("disposalCountId",outStockService.salesCountId(id,b,e,0));
                // 销售额
                if(t != null) modelAndView.addObject("salesTotalId",outStockService.salesTotalId(id,b,e));

                // 生产量
                if(p != null)  modelAndView.addObject("totalProductionByParam",inStockService.totalProductionByParam(id,b,e));
                // 过期
                if (o != null) modelAndView.addObject("outOfDateByParam", existingService.outDateCountByIdAndDate(id,e));
                // 库存
                if (i != null) modelAndView.addObject("inventoryByParam", existingService.existCountByParam(id,e));
                if(id.compareTo("000000") != 0)
                    modelAndView.addObject("id",id);
                else modelAndView.addObject("id","ALL");

                if(typeId.compareTo("000") != 0){  // 种类
                    if(c != null) modelAndView.addObject("salesCountType",outStockService.salesCountType(typeId,b,e,1));
                    if(d != null) modelAndView.addObject("disposalCountType",outStockService.salesCountType(typeId,b,e,0));
                    if(t != null) modelAndView.addObject("salesTotalType",outStockService.salesTotalType(typeId,b,e));

                    // 生产量
                    if(p != null) modelAndView.addObject("totalProductionByType",inStockService.totalProductionByType(typeId,b,e));
                    // 过期
                    if(o != null) modelAndView.addObject("outOfDateByType",existingService.outDateCountByTypeAndDate(typeId,e));
                    modelAndView.addObject("idt",typeId);
                    // 库存
                    if(i != null) modelAndView.addObject("inventoryByType",existingService.existCountByType(typeId,e));
                }

            }
            /*
            else if(id.compareTo("000000") != 0 || typeId.compareTo("000") != 0){
                if(id.compareTo("000000") != 0) {
                    // 库存
                    if (i != null) modelAndView.addObject("inventoryByParam", existingService.inventoryByParam(id));
                }
                if(typeId.compareTo("000") != 0){
                    // 库存
                    if(i != null) modelAndView.addObject("inventoryByType",existingService.inventoryByType(typeId));

                }
            }
            */
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return modelAndView;


    }
}
